using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using Volo.Abp.Application.Dtos;

namespace Matrix.Configuration.DTOs
{
    /// <summary>
    /// 配置项DTO
    /// </summary>
    public class ConfigurationItemDto : EntityDto<Guid>
    {
        /// <summary>
        /// 命名空间
        /// </summary>
        public string Namespace { get; set; } = string.Empty;

        /// <summary>
        /// 配置键名
        /// </summary>
        public string Key { get; set; } = string.Empty;

        /// <summary>
        /// 配置值
        /// </summary>
        public string Value { get; set; } = string.Empty;

        /// <summary>
        /// 配置分类
        /// </summary>
        public string Category { get; set; } = string.Empty;

        /// <summary>
        /// 配置描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 数据类型
        /// </summary>
        public string DataType { get; set; } = "string";

        /// <summary>
        /// 配置版本
        /// </summary>
        public int Version { get; set; } = 1;

        /// <summary>
        /// 租户标识
        /// </summary>
        public Guid? TenantId { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        public DateTime? LastModificationTime { get; set; }
    }

    /// <summary>
    /// 创建配置项DTO
    /// </summary>
    public class CreateConfigurationItemDto
    {
        /// <summary>
        /// 配置键名
        /// </summary>
        [Required(ErrorMessage = "配置键名不能为空")]
        [StringLength(ConfigurationItemConsts.MaxKeyLength, ErrorMessage = "配置键名长度不能超过{1}")]
        public string Key { get; set; } = string.Empty;

        /// <summary>
        /// 配置值
        /// </summary>
        [Required(ErrorMessage = "配置值不能为空")]
        [StringLength(ConfigurationItemConsts.MaxValueLength, ErrorMessage = "配置值长度不能超过{1}")]
        public string Value { get; set; } = string.Empty;

        /// <summary>
        /// 配置分类
        /// </summary>
        public string Category { get; set; } = string.Empty;

        /// <summary>
        /// 配置描述
        /// </summary>
        [StringLength(ConfigurationItemConsts.MaxDescriptionLength, ErrorMessage = "描述长度不能超过{1}")]
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 命名空间
        /// </summary>
        public string Namespace { get; set; } = "";

        /// <summary>
        /// 数据类型
        /// </summary>
        public string DataType { get; set; } = ConfigurationItemConsts.DefaultDataType;
    }

    /// <summary>
    /// 更新配置项DTO
    /// </summary>
    public class UpdateConfigurationItemDto
    {
        /// <summary>
        /// 配置值
        /// </summary>
        [StringLength(ConfigurationItemConsts.MaxValueLength, ErrorMessage = "配置值长度不能超过{1}")]
        public string? Value { get; set; }

        /// <summary>
        /// 配置分类
        /// </summary>
        public string? Category { get; set; }

        /// <summary>
        /// 配置描述
        /// </summary>
        [StringLength(ConfigurationItemConsts.MaxDescriptionLength, ErrorMessage = "描述长度不能超过{1}")]
        public string? Description { get; set; }

        /// <summary>
        /// 数据类型
        /// </summary>
        public string? DataType { get; set; }
    }

    /// <summary>
    /// 获取配置项列表输入参数
    /// </summary>
    public class GetConfigurationItemListInput : PagedAndSortedResultRequestDto
    {
        /// <summary>
        /// 命名空间过滤
        /// </summary>
        public string? Namespace { get; set; }

        /// <summary>
        /// 配置键名过滤
        /// </summary>
        public string? Key { get; set; }

        /// <summary>
        /// 配置值过滤
        /// </summary>
        public string? Value { get; set; }

        /// <summary>
        /// 配置分类过滤
        /// </summary>
        public string? Category { get; set; }

        /// <summary>
        /// 配置描述过滤
        /// </summary>
        public string? Description { get; set; }

        /// <summary>
        /// 数据类型过滤
        /// </summary>
        public List<string>? DataTypes { get; set; }
    }

    /// <summary>
    /// 获取配置项输入参数
    /// </summary>
    public class GetConfigurationItemInput
    {
        /// <summary>
        /// 配置键名
        /// </summary>
        [Required(ErrorMessage = "配置键名不能为空")]
        public string Key { get; set; } = string.Empty;

        /// <summary>
        /// 命名空间
        /// </summary>
        public string Namespace { get; set; } = "";
    }

    // ==================== 批量操作结果DTO ====================

    /// <summary>
    /// 批量操作结果项DTO
    /// </summary>
    public class BatchOperationItemResultDto
    {
        /// <summary>
        /// 配置键名
        /// </summary>
        public string Key { get; set; } = string.Empty;

        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 配置项DTO（成功时返回）
        /// </summary>
        public ConfigurationItemDto? Item { get; set; }
    }

    /// <summary>
    /// 批量操作结果DTO
    /// </summary>
    public class BatchOperationResultDto
    {
        /// <summary>
        /// 操作结果项列表
        /// </summary>
        public List<BatchOperationItemResultDto> Items { get; set; } = new();

        /// <summary>
        /// 成功数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 总数量
        /// </summary>
        public int TotalCount => SuccessCount + FailureCount;
    }

    /// <summary>
    /// 批量导入结果DTO
    /// </summary>
    public class BatchImportResultDto
    {
        /// <summary>
        /// 总处理数量
        /// </summary>
        public int TotalProcessed { get; set; }

        /// <summary>
        /// 成功数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 跳过数量（已存在且不覆盖）
        /// </summary>
        public int SkippedCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 操作结果项列表
        /// </summary>
        public List<BatchOperationItemResultDto> Items { get; set; } = new();
    }

    /// <summary>
    /// 批量导出结果DTO
    /// </summary>
    public class BatchExportResultDto
    {
        /// <summary>
        /// 导出的配置项列表
        /// </summary>
        public List<ConfigurationItemDto> Items { get; set; } = new();

        /// <summary>
        /// 导出时间
        /// </summary>
        public DateTime ExportTime { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 命名空间
        /// </summary>
        public string? Namespace { get; set; }
    }

    // ==================== 批量操作输入DTO ====================

    /// <summary>
    /// 批量创建配置项输入DTO
    /// </summary>
    public class BatchCreateConfigurationItemDto
    {
        /// <summary>
        /// 配置项列表
        /// </summary>
        [Required(ErrorMessage = "配置项列表不能为空")]
        public List<CreateConfigurationItemDto> Items { get; set; } = new();

        /// <summary>
        /// 是否在错误时回滚
        /// </summary>
        public bool RollbackOnError { get; set; } = false;

        /// <summary>
        /// 命名空间（可选，如果不指定则使用每个配置项的命名空间）
        /// </summary>
        public string? Namespace { get; set; }
    }

    /// <summary>
    /// 批量更新配置项输入DTO
    /// </summary>
    public class BatchUpdateConfigurationItemDto
    {
        /// <summary>
        /// 更新项列表
        /// </summary>
        [Required(ErrorMessage = "更新项列表不能为空")]
        public List<UpdateConfigurationItemDto> Items { get; set; } = new();

        /// <summary>
        /// 命名空间
        /// </summary>
        [Required(ErrorMessage = "命名空间不能为空")]
        public string Namespace { get; set; } = string.Empty;

        /// <summary>
        /// 是否在错误时回滚
        /// </summary>
        public bool RollbackOnError { get; set; } = false;
    }

    /// <summary>
    /// 批量删除配置项输入DTO
    /// </summary>
    public class BatchDeleteConfigurationItemDto
    {
        /// <summary>
        /// 配置键名列表
        /// </summary>
        [Required(ErrorMessage = "配置键名列表不能为空")]
        public List<string> Keys { get; set; } = new();

        /// <summary>
        /// 命名空间
        /// </summary>
        [Required(ErrorMessage = "命名空间不能为空")]
        public string Namespace { get; set; } = string.Empty;

        /// <summary>
        /// 是否在错误时回滚
        /// </summary>
        public bool RollbackOnError { get; set; } = false;
    }

    /// <summary>
    /// 批量导入配置项输入DTO
    /// </summary>
    public class BatchImportConfigurationItemDto
    {
        /// <summary>
        /// 配置项列表
        /// </summary>
        [Required(ErrorMessage = "配置项列表不能为空")]
        public List<CreateConfigurationItemDto> Items { get; set; } = new();

        /// <summary>
        /// 是否覆盖已存在的配置项
        /// </summary>
        public bool OverwriteExisting { get; set; } = false;

        /// <summary>
        /// 是否在错误时回滚
        /// </summary>
        public bool RollbackOnError { get; set; } = false;

        /// <summary>
        /// 命名空间（可选，如果不指定则使用每个配置项的命名空间）
        /// </summary>
        public string? Namespace { get; set; }
    }

    /// <summary>
    /// 批量导出配置项输入DTO
    /// </summary>
    public class BatchExportConfigurationItemDto
    {
        /// <summary>
        /// 命名空间（可选，如果不指定则导出所有命名空间）
        /// </summary>
        public string? Namespace { get; set; }

        /// <summary>
        /// 配置分类过滤（可选）
        /// </summary>
        public string? Category { get; set; }

        /// <summary>
        /// 是否包含元数据
        /// </summary>
        public bool IncludeMetadata { get; set; } = true;

        /// <summary>
        /// 数据类型过滤（可选）
        /// </summary>
        public List<string>? DataTypes { get; set; }
    }

    /// <summary>
    /// 按前缀批量删除输入DTO
    /// </summary>
    public class BatchDeleteByPrefixDto
    {
        /// <summary>
        /// 命名空间
        /// </summary>
        [Required(ErrorMessage = "命名空间不能为空")]
        public string Namespace { get; set; } = string.Empty;

        /// <summary>
        /// 配置键名前缀
        /// </summary>
        [Required(ErrorMessage = "配置键名前缀不能为空")]
        public string KeyPrefix { get; set; } = string.Empty;

        /// <summary>
        /// 是否在错误时回滚
        /// </summary>
        public bool RollbackOnError { get; set; } = false;
    }

    /// <summary>
    /// 按前缀批量删除结果DTO
    /// </summary>
    public class BatchDeleteByPrefixResultDto
    {
        /// <summary>
        /// 删除的配置项数量
        /// </summary>
        public int DeletedCount { get; set; }

        /// <summary>
        /// 删除的配置键名列表
        /// </summary>
        public List<string> DeletedKeys { get; set; } = new();

        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool Success { get; set; } = true;

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }
    }

    /// <summary>
    /// 批量操作配置项DTO
    /// </summary>
    public class BatchConfigurationItemDto
    {
        /// <summary>
        /// 配置键名
        /// </summary>
        public string Key { get; set; } = string.Empty;

        /// <summary>
        /// 配置值
        /// </summary>
        public string Value { get; set; } = string.Empty;

        /// <summary>
        /// 配置分类
        /// </summary>
        public string Category { get; set; } = string.Empty;

        /// <summary>
        /// 配置描述
        /// </summary>
        public string? Description { get; set; }

        /// <summary>
        /// 命名空间
        /// </summary>
        public string Namespace { get; set; } = "";

        /// <summary>
        /// 数据类型
        /// </summary>
        public string? DataType { get; set; }
    }

    /// <summary>
    /// 配置项常量
    /// </summary>
    public static class ConfigurationItemConsts
    {
        /// <summary>
        /// 命名空间最大长度
        /// </summary>
        public const int MaxNamespaceLength = 128;

        /// <summary>
        /// 配置键名最大长度
        /// </summary>
        public const int MaxKeyLength = 256;

        /// <summary>
        /// 配置名称最大长度
        /// </summary>
        public const int MaxNameLength = 256;

        /// <summary>
        /// 配置值最大长度
        /// </summary>
        public const int MaxValueLength = 4000;

        /// <summary>
        /// 描述最大长度
        /// </summary>
        public const int MaxDescriptionLength = 500;

        /// <summary>
        /// 默认数据类型
        /// </summary>
        public const string DefaultDataType = "string";
    }
}